package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysConfigDao;
import ysq.xyg.sys.model.po.SysConfig;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysConfigVo;
import ysq.xyg.sys.service.SysConfigService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 系统管理-通用配置service实现
 *
 * @author shengQiang yu
 * @date 2019-3-28 16:42:12
 */
@Service("sysConfigService")
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfig> implements SysConfigService {

    @Resource
    private SysConfigDao sysConfigDao;

    private SysConfig sysConfig;

    @Autowired
    public void setBaseDao(SysConfigDao sysConfigDao) {
        this.baseDao = sysConfigDao;
    }


    /**
     * 分页查询
     *
     * @param sysConfigVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-28 16:42:12
     */
    public ReturnEntity page(SysConfigVo sysConfigVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SysConfig> list = acceptList(sysConfigVo);
        PageInfo<SysConfig> page=new PageInfo<SysConfig>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param sysConfigVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-28 16:42:12
     */
    public ReturnEntity addOrUpdate(SysConfigVo sysConfigVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysConfigVo.getId())){
            sysConfig=new SysConfig(currentUser);
            isUpate=false;
        }else{
            sysConfig=selectByPrimaryKey(sysConfigVo.getId());
            if(ObjectUtil.isNull(sysConfig)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysConfig.setUpdateTime(new Date());
            sysConfig.setUpdateUser(currentUser.getId());
        }
            sysConfig.setName(sysConfigVo.getName());
            sysConfig.setContent(sysConfigVo.getContent());
            sysConfig.setRemark(sysConfigVo.getRemark());

        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysConfig);
        }else{
            num=insert(sysConfig);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-28 16:42:12
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysConfig sysConfig=selectByPrimaryKey(id);
        if(sysConfig!=null){
            sysConfig.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            sysConfig.setUpdateTime(new Date());
            sysConfig.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(sysConfig);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param sysConfigVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-3-28 16:42:12
     */
    public List<SysConfig> acceptList(SysConfigVo sysConfigVo){
        Example example = new Example(SysConfig.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysConfigVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysConfigVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return sysConfigDao.selectByExample(example);
    }
}
